﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using System.Threading;

namespace Hisun.View.Calculation
{
    public partial class Close : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Close_Click(object sender, EventArgs e)
        {



            if (db.Connection != null)
            {
                db.Connection.Open();

            }

            System.Data.Common.DbTransaction tran = db.Connection.BeginTransaction();


            db.Transaction = tran;

            //该方法实现了一个图标在计算执行的过程中的等待界面。
            //插入
            try
            {
            string CurrentAccountPeriod = Application["accountPeriod"].ToString();

            Response.Write("<div id=xx2  align = center ><img alt='' src='/App_Themes/2011_Blue/Images/Loading.gif' width = 100 height = 100 /><br>正在计算，不要关闭窗口，请等待。。。</div>");
            string MyEmptyString = " ";
            
                for (int i = 0; i < 300; i++)           
                {
           
                    MyEmptyString = MyEmptyString + " ";
           
                }
            Response.Write(MyEmptyString);//用于填充满256个字节。
            Response.Flush();

            Response.Write("<div  align = center><br>正在结账，请等待。。。</div>");
            db.数据结转(CurrentAccountPeriod);


            var MyaccountPeriod = (from a in db.TABC_JS_AccountPeriods
                                 where a.会计期间ID == CurrentAccountPeriod
                                 select a).Single();
            MyaccountPeriod.成本结账标志 = true;
            db.SubmitChanges();

            String NextAccountPeriod=string.Empty;
            db.取会计期间ID(1, CurrentAccountPeriod, ref NextAccountPeriod);
                
            var MyNextaccountPeriod = (from a in db.TABC_JS_AccountPeriods
                                   where a.会计期间ID == NextAccountPeriod
                                   select a).Single();
            MyNextaccountPeriod.成本结账标志 = false;
            db.SubmitChanges();


            Application["accountPeriod"] = NextAccountPeriod;

            Response.Write("<div  align = center><br>完成，请重新登录系统!</div>");

            //db.产品原材料计算(Application["accountPeriod"].ToString());
            Response.Write("<script>document.getElementById('xx2').style.display='none';</script>");


            tran.Commit();
            
            }
            catch 
            {
                tran.Rollback();
            
            }


            
 
        }
    }
}
